package com.xiaoma.bingo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dexcoder.commons.pager.Pager;
import com.dexcoder.dal.JdbcDao;
import com.dexcoder.dal.spring.page.PageControl;
import com.xiaoma.bingo.entity.User;

@Service
public class UserService {

    @Autowired
    private JdbcDao jdbcDao;
    
    public Pager findAll(int currentPage, int itemsPerPage) {
        PageControl.performPage(currentPage, itemsPerPage);
        String sql = "select a.id, a.name, a.password, a.age, a.email, a.created_at, a.role_id, b.role from user a left join role b on a.role_id = b.id order by a.id asc";
        jdbcDao.queryListForSql(sql);
        return PageControl.getPager();
        
    }
    
    public List<User> find(User entity) {
        return jdbcDao.queryList(entity);
    }
    
    public void save(User user) {
        jdbcDao.save(user);
    }
    
    
    public void delete(Integer id) {
        jdbcDao.delete(User.class, id);
    }
    
    public void update(User user) {
        jdbcDao.update(user);
    }
    
    public User getById(Integer id) {
        return jdbcDao.get(User.class, id);
    }
    
    public User findByNameAndPwd(String name, String password) {
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        return jdbcDao.querySingleResult(user);
    }
}
